<?php
include "./fun.php";
$db=conndb();
$list=get_Children_Class(17, $db);
$sortid=isset($_GET["sortid"])?$_GET["sortid"]:'';
$id=isset($_GET["id"])?$_GET["id"]:'';
$page=isset($_GET["page"])?$_GET["page"]:1;
$limit=isset($_GET["limit"])?$_GET["limit"]:8;
// $type=$_GET["type"];
$keyword=isset($_GET["keyword"])?$_GET["keyword"]:'';
// news_detail
// 自动更新浏览次数
if ($id) {
    $update="update news set views=views+1 where id='{$id}'";
    $db->exec($update);
    $sql_detail="select * from news where id='{$id}'";
    $res_detail=query1($db, $sql_detail, false);
    $newsSortid=isset($res_detail["sortid"])?$res_detail["sortid"]:'';
    // 上一篇
    $pre_sql="select * from news where id<'{$id}' and sortid='{$newsSortid}' order by id desc limit 1";
    $pre=query1($db, $pre_sql, false);

    // 下一篇
    $next_sql="select * from news where id>'{$id}' and sortid='{$newsSortid}' order by id asc limit 1";
    $next=query1($db, $next_sql, false);

    $detail="select * from sortclass where id='{$newsSortid}'";
    $name=query1($db, $detail, false);
    $data=["code"=>0,"msg"=>"操作成功","list"=>$list,"detail"=>$res_detail,"name"=>$name["sortname"],"pre"=>$pre,"next"=>$next];
    echo json_encode($data, JSON_UNESCAPED_UNICODE);
    die();
}
$where='';
if ($sortid) {
    $where.="and sortid='{$sortid}'";
}
if ($keyword) {
    $where.="and news_name like '%{$keyword}%'";
}
if ($where) {
    $where="where ".substr($where, 3);
}
$sql_count="select count(*) as total from news {$where} ";
$re=query1($db, $sql_count, false);
$count=$re["total"];
$start=($page-1)*$limit;
$sql="select * from news {$where} order by id asc limit {$start},{$limit}";
$res=query1($db, $sql);
$sql_name="select sortname from sortclass where id='{$sortid}'";
$re_name=query1($db, $sql_name, false);
$data=["code"=>0,"msg"=>"操作成功","data"=>$res,"list"=>$list,"page"=>$page,"count"=>$count,"limit"=>$limit,"sortid"=>$sortid,"sortname"=>$re_name["sortname"],];
echo json_encode($data, JSON_UNESCAPED_UNICODE);

// $update="update news set views=views+1 where id='{$id}'";
// $db->exec($update);
// $sql_detail="select * from news where id='{$id}'";
// $res_detail=query1($db,$sql_detail);
// // 上一篇
// $pre_sql="select * from news where id<'{$id}' and sortid='{$res_detail[0]["sortid"]}' order by id desc limit 1";
// $pre=query1($db,$pre_sql,false);

// // 下一篇
// $next_sql="select * from news where id>'{$id}' and sortid='{$res_detail[0]["sortid"]}' order by id asc limit 1";
// $next=query1($db,$next_sql,false);

// $detail="select * from sortclass where id='{$res_detail[0]["sortid"]}'";
// $name=query1($db,$detail,false);
// $data=["code"=>0,"msg"=>"操作成功","data"=>$res,"list"=>$list,"detail"=>$res_detail,"page"=>$page,"count"=>$count,"sortid"=>$sortid,"sortname"=>$re_name["sortname"],"name"=>$name["sortname"],"pre"=>$pre,"next"=>$next];
// echo json_encode($data,JSON_UNESCAPED_UNICODE);
